<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['class:class_choose:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['class:class_choose:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['class:class_choose:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['class:class_choose:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="class_chooseList" @selection-change="handleSelectionChange">
      <el-table-column type="index" label="序号" align="center" width="50"/>
      <el-table-column label="课程名" align="center" prop="class_name" />
      <el-table-column label="教师" align="center" prop="user_name" />
      <el-table-column label="专业" align="center" prop="dept_name" />
      <el-table-column label="学时" align="center" prop="class_time" />
      <el-table-column label="学分" align="center" prop="class_crs" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="chooseClass(scope.row)"
            v-hasPermi="['student:choose']"
          >选课</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['student:out1']"-->
<!--          >退课</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改class_choose对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入学生id" />
        </el-form-item>
        <el-form-item label="课程id" prop="classId">
          <el-input v-model="form.classId" placeholder="请输入课程id" />
        </el-form-item>
        <el-form-item label="已获学分" prop="classScore">
          <el-input v-model="form.classScore" placeholder="请输入已获学分" />
        </el-form-item>
        <el-form-item label="学习进度" prop="classProgress">
          <el-input v-model="form.classProgress" placeholder="请输入学习进度" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {
  listClass_choose_s,
  getClass_choose,
  delClass_choose,
  addClass_choose,
  updateClass_choose,
  insertCLass
} from "@/api/class/class_choose"

export default {
  name: "Class_choose",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // class_choose表格数据
      class_chooseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        classId: null,
        classScore: null,
        classProgress: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "学生id不能为空", trigger: "blur" }
        ],
        classId: [
          { required: true, message: "课程id不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询class_choose列表 */
    getList() {
      this.loading = true
      listClass_choose_s(this.queryParams).then(response => {
        this.class_chooseList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        classId: null,
        classScore: null,
        classProgress: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加class_choose"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      getClass_choose(row.id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改class_choose"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateClass_choose(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addClass_choose(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除class_choose编号为"' + row.class_id + '"的数据项？').then(function () {
        return delClass_choose(row.class_id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('class/class_choose/export', {
        ...this.queryParams
      }, `class_choose_${new Date().getTime()}.xlsx`)
    },
    /** 选课按钮操作 */
    chooseClass(row) {
      this.reset()
      this.$modal.confirm('是否确认选择"' + row.class_name  + '课程？').then(() => {// 假设userId从store中获取
        return insertCLass(row.class_id)
      }).then(async () => {
        this.getList()
        this.$modal.msgSuccess("选择成功")

        const response = await this.$request.post('/ai/get_pic', {class_name:  row.class_name,class_id:row.class_id});

        console.log(response)

      }).catch(() => {
      }).catch((error) => {
        if (error.response && error.response.data && error.response.data.message === "课程已选") {
          this.$modal.msgError("课程已选");
        } else {
          this.$modal.msgError("选课失败，请稍后重试");
        }
      });
    }



  }
}
</script>
